<template>
  <div v-if="isVisible" class="modal-overlay" @click.self="handleOverlayClick">
    <div class="modal-content">
      <div class="modal-header">
        <h3>会议室报修</h3>
        <button class="close-button" @click="cancelReport">&times;</button>
      </div>
      <div class="modal-body">
        <form @submit.prevent="submitReport">
          <div class="form-group">
            <label for="roomIdDisplay">会议室 ID:</label>
            <input id="roomIdDisplay" type="text" :value="roomId" readonly class="form-control-readonly" />
          </div>
          <div class="form-group">
            <label for="problemDesc">问题描述:</label>
            <textarea
              id="problemDesc"
              v-model.trim="problemDescription"
              rows="5"
              placeholder="请详细描述会议室存在的问题或损坏情况"
              required
            ></textarea>
            <small v-if="descriptionError" class="error-text">{{ descriptionError }}</small>
          </div>
          <div class="modal-actions">
            <button type="button" class="btn btn-secondary" @click="cancelReport">
              取消
            </button>
            <button type="submit" class="btn btn-primary">
              提交报修
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, defineProps, defineEmits } from 'vue';

const props = defineProps({
  isVisible: {
    type: Boolean,
    required: true,
  },
  roomId: {
    type: Number,
    default: null,
  },
});

const emits = defineEmits(['submit', 'cancel']);

const problemDescription = ref('');
const descriptionError = ref('');

// Watch for isVisible prop to reset form when modal opens
watch(() => props.isVisible, (newVal) => {
  if (newVal) {
    problemDescription.value = ''; // Reset description
    descriptionError.value = '';   // Reset error message
  }
});

const validateDescription = () => {
  if (!problemDescription.value) {
    descriptionError.value = '问题描述不能为空。';
    return false;
  }
  if (problemDescription.value.length < 5) {
    descriptionError.value = '问题描述至少需要5个字符。';
    return false;
  }
  descriptionError.value = '';
  return true;
};

const submitReport = () => {
  if (validateDescription()) {
    emits('submit', { problemDesc: problemDescription.value });
  }
};

const cancelReport = () => {
  emits('cancel');
};

// Optional: Allow closing modal by clicking on the overlay
const handleOverlayClick = () => {
  // You might want to confirm with the user or make this configurable
  // For simplicity, directly emitting cancel here.
  // cancelReport();
};

</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.6);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000; /* Ensure it's above other content */
}

.modal-content {
  background-color: #fff;
  padding: 25px;
  border-radius: 8px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
  width: 90%;
  max-width: 500px;
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #e9ecef;
  padding-bottom: 15px;
  margin-bottom: 20px;
}

.modal-header h3 {
  margin: 0;
  font-size: 1.5em;
  color: #333;
}

.close-button {
  background: none;
  border: none;
  font-size: 1.8em;
  cursor: pointer;
  color: #666;
  padding: 0 5px;
}
.close-button:hover {
  color: #333;
}

.modal-body {
  flex-grow: 1;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: bold;
  color: #555;
}

.form-group textarea,
.form-group input[type="text"].form-control-readonly {
  width: 100%;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 1em;
  box-sizing: border-box; /* Ensures padding doesn't add to width */
}

.form-control-readonly {
  background-color: #f8f9fa;
  cursor: default;
}

.form-group textarea {
  resize: vertical; /* Allow vertical resizing */
  min-height: 100px;
}

.error-text {
  color: #dc3545;
  font-size: 0.875em;
  margin-top: 5px;
  display: block;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px; /* Space between buttons */
  margin-top: 25px;
  padding-top: 15px;
  border-top: 1px solid #e9ecef;
}

.btn {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1em;
  font-weight: 500;
  transition: background-color 0.2s ease, box-shadow 0.2s ease;
}

.btn-primary {
  background-color: #007bff;
  color: white;
}
.btn-primary:hover {
  background-color: #0056b3;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.btn-secondary {
  background-color: #6c757d;
  color: white;
}
.btn-secondary:hover {
  background-color: #545b62;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
</style>